﻿/*
 * ========================================================================
 * Copyright(c) 2007-2012 情留メ蚊子, All Rights Reserved.
 * Welcom use the LiteFetion.
 * See more information,Please goto http://www.94qing.com
 * ========================================================================
 * 
 * 作 者：情留メ蚊子
 * Q  Q: 540644769
 * 邮 箱：qlwz@qq.com
 * 网 址：http://www.94qing.com
 * ========================================================================
*/
using System;
using LiteFetion.Model;
using Newtonsoft.Json.Linq;

namespace LiteFetion.Notify
{
    /// <summary>
    /// 好友状态通知
    /// </summary>
    public class BuddyStateNotify
    {
        public BuddyStateNotify(FetionClient client, JObject data)
        {
            this.FetionClient = client;
            int userId = data.Value<int>("uid");
            this.Buddy = this.FetionClient.BuddyManager.GetBuddyByUserId(userId);
            if (this.Buddy != null)
            {
                this.BeforeState = this.Buddy.BuddyState;
                if (data["nn"] != null)
                {
                    this.Buddy.NickName = data.Value<string>("nn");
                }
                if (data["mn"] != null)
                {
                    this.Buddy.Mobile = data.Value<string>("mn");
                }
                if (data["i"] != null)
                {
                    this.Buddy.Impresa = data.Value<string>("i");
                }
                if (data["sms"] != null)
                {
                    this.Buddy.SMSPolicy.Parse(data.Value<string>("sms"));
                }
                if (data["sid"] != null)
                {
                    this.Buddy.Sid = data.Value<int>("sid");
                }
                if (data["pb"] != null)
                {
                    this.Buddy.BuddyState = Buddy.GetBuddyState(data.Value<string>("pb").ToInt());
                }
                if (data["crc"] != null)
                {
                    this.Buddy.Crc = data.Value<string>("crc");
                }
                this.CurrentState = this.Buddy.BuddyState;
                Event.OnBuddyStateNotify(this);
            }
        }

        /// <summary>
        /// FetionClient
        /// </summary>
        public FetionClient FetionClient { get; private set; }

        /// <summary>
        /// 之前的状态
        /// </summary>
        public BuddyState BeforeState { get; private set; }

        /// <summary>
        /// 现在的状态
        /// </summary>
        public BuddyState CurrentState { get; private set; }

        /// <summary>
        /// 好友对象
        /// </summary>
        public Buddy Buddy { get; private set; }

        public override string ToString()
        {
            return "BuddyStateNotify [Buddy=" + this.Buddy.DisplayName + ", BeforeState="
                + this.BeforeState + ", CurrentState=" + this.CurrentState + "]";
        }
    }
}
